package com.example.cart.repository;

import android.database.Cursor;
import android.hardware.lights.LightState;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.example.cart.dto.Cart;
import com.example.cart.dto.CartWithGoods;

import java.util.List;

@Dao
public interface CartDao {

    @Insert
    void insert(Cart cart);

    @Delete
    void delete(Cart cart);

    @Update
    void Update(Cart cart);

    @Query("select * from cart")
    List<Cart> list();

    /**
     * <a href="https://developer.android.com/training/data-storage/room/relationships?hl=zh-cn#java">多表查询参考文档</a>
     *
     * @param offset
     * @param size
     * @return
     */
    @Query("select goods.*,cart.quantity as cartQuantity,cart.id as cartId from cart left join goods on cart.goods_id = goods.id limit :offset,:size")
    List<CartWithGoods> page(int offset, int size);

    @Query("select goods.name,cart.id,cart.quantity as cartQuantity,cart.id as cartId from cart left join goods on cart.goods_id = goods.id")
    Cursor all();

    @Query("select * from cart where goods_id = :goodsId limit 1")
    Cart queryById(int goodsId);

    @Query("select sum(quantity) from cart")
    int count();


}
